TGGS Computer Science

Y7 – Spring 1 – Lesson 2: While Loops for Validation

Click on the relevant section to read the instructions


Do Now

🕛 Do Now: Fix the Code


  • The code below contains three errors
  • Fix the errors so the code works correctly
  • Explain your changes in the comments

Coding Activity 1

🎓 Coding Activity 1: Password Length Check


  1. Ask the user to enter a password
  2. Use a while loop to check the password length
  3. Repeat until the password is at least 8 characters long
Extension Task
See if you can get the code to allow for passwords within a specific range e.g. between 8 and 25 characters.

In Python, two or more conditions can be tested together at the same time...
num >= 3 and num <= 10

Coding Activity 2

🎓 Coding Activity 2: Email Presence Check


  1. Ask the user for an email address
  2. Check the email contains the @ symbol
  3. Repeat until valid input is entered
Extension Task 1
See if you can get the code to also check that .org is present in the email address

In Python, two or more conditions can be tested together at the same time...
"abc" in string and "def" in string

Extension Task 2
If you have completed extension task 1, you might notice that when you run the code you are able to simply input "@.org" for it to be valid

This is five characters long.

Add another check to your condition to ensure the input is longer than this. I would suggest at least 9 characters.
"abc" in string and "def" in string and --your new condition--

Coding Activity 3

🎓 Coding Activity 3: Type Check – Age Validation


  1. Ask the user to enter their age
  2. Check the input is an integer
  3. Repeat until valid input is entered

END OF LESSON EXTENSION
See if you can create a program that asks the user to enter their name, age and email address.

  • Each will be stored as a separate variable.
  • Appropriate validation is carried out after each user input